#include("/admin/common/header.html")
#include("/admin/common/footer.html")
#include("/admin/common/left.html")
#include("/admin/common/layout.html")
 
 #@layout()
 #define content()

<span class="layui-breadcrumb">
  <a href="#(ctx_path)/">首页</a>
  <a href="#(ctx_path)/admin/shop/product"><cite>商品管理</cite></a>
</span>

<hr>


<div class="layui-field-box">
<form class="layui-form" id="searchForm">

	<div class="layui-form-item">
		<div class="layui-inline" id="treeSelect">
			<label class="layui-form-label">商品类别</label>
		</div>
	</div>

    <div class="layui-form-item">

		<div class="layui-inline">
			<label class="layui-form-label">搜索内容</label>
			<div class="layui-input-inline">
				<input type="text" name="searchContent" autocomplete="off" class="layui-input" placeholder="请输入内容搜索">
			</div>
		</div>

		<div class="layui-inline">
			<label class="layui-form-label">商品状态</label>
			<div class="layui-input-inline">
					#set(DIC_MAP={})
					#set(DIC_MAP["dicTypeCode"]="SHOP_PRODUCT_STATUS")
					#set(DIC_MAP["name"]="status")
					#set(DIC_MAP["id"]="status")
				    #@dic_get_select(DIC_MAP)
			</div>
		</div>
	    
	    <button class="layui-btn" type="button" id="btnSearch">搜索</button>
	    <button class="layui-btn layui-btn-normal" type="reset" >重置</button>
	    #auth('USER_ADD')
			<a class="layui-btn layui-btn-danger" href="#(ctx_path)/admin/shop/product/add" >新增</a>
		#end
	   
	    
	</div>
</form>	    
</div>
  
<table class="layui-hide" id="dataTable"></table>


<script type="text/html" id="actionTpl">

  	<a href="javascript:doDetail('{{d.id}}');" class="layui-table-link" actionType="detail" actionId="{{d.id}}">详情</a>
  	<a href="javascript:doStartUsing('{{d.id}}');" class="layui-table-link" >上架</a>
  	<a href="javascript:doForbidden('{{d.id}}');" class="layui-table-link" >下架</a>
	
</script>

#end

#define js()

<script>


	layui.use('table', function() {
		
		var table = layui.table;
		table.render({
			elem : '#dataTable',
			url : '#(ctx_path)/admin/shop/product/pagequery',
			cols : [ [
		      {field:'name', title: '型号（名称）'},
		      {field:'createTime', title: '创建时间', sort: true},
		      {field:'status_dic', title: '状态'},
		      {templet:"#actionTpl",width:'180',title: '操作'}
			] ],
			page : true
		});
		
		$("#btnSearch").click(function(){
			
			var categoryId=$(":input[name='categoryId']").val();
			var searchContent=$(":input[name='searchContent']").val();
			var status=$(":input[name='status']").val();
			
			//执行重载
			table.reload('dataTable', {
				page : {
					curr : 1//重新从第 1 页开始
				},
				where : {
					searchContent:searchContent,
					status:status,
					categoryId:categoryId
				}
			});
			
		});

	});

	function doDetail(id) {
		window.location.href = "#(ctx_path)/admin/shop/product/view?id=" + id;
	}

	function doStartUsing(id) {
		layui.layer.confirm('确认上架么?', function(index) {
			$.ajax({
				type : 'get',
				url : '#(ctx_path)/admin/shop/product/publish',
				data : 'id=' + id,
				success : function(data) {
					layui.table.reload('dataTable');
				}
			});
			layui.layer.close(index);
		});
	}

	function doForbidden(id) {
		layui.layer.confirm('确认下架么?', function(index) {
			$.ajax({
				type : 'get',
				url : '#(ctx_path)/admin/shop/product/unpublish',
				data : 'id=' + id,
				success : function(data) {
					layui.table.reload('dataTable');
				}
			});
			layui.layer.close(index);
		});
	}

	var nodes=[];

	function initTree(){
		$("#treeElem").html("");
		$.ajax({
			type:'get',
			url:'#(ctx_path)/admin/shop/category/tree',
			data:'',
			success:function(data){
				if(data.code=='0'){

					nodes=data.data;
					
					var categoryId=$(":input[name='categoryId']").val();
					if(categoryId){
						initForm(categoryId);
					}else{
						initForm();
					}

				}
			}
		});
	}

	function initForm(categoryId){
		layui.use('treeSelect',function(){
			var treeSelect=layui.treeSelect;
			treeSelect.init("#treeSelect",{
				nodes:nodes,
				data:categoryId,
			});
		});
		
	}

	$(function(){
		initTree();
	});


</script>

#end


